
package com.eltonkent.ui;

import java.util.ArrayList;

import android.app.Activity;
import android.app.Dialog;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.eltonkent.R;

abstract class ParentActivity extends Activity
{
	private TextView stausText;
	private ProgressBar progress;
	private static final int EXIT_APP_DIALOG = 150;

	private View.OnClickListener quitButtonClickListener = new View.OnClickListener()
	{

		@Override
		public void onClick(View v)
		{
			Log.d("UI", "Quit Button clicked");
			showDialog(EXIT_APP_DIALOG);

		}
	};

	private View.OnClickListener yesListener = new View.OnClickListener()
	{

		@Override
		public void onClick(View v)
		{
			System.exit(0);
		}
	};

	private View.OnClickListener noListener = new View.OnClickListener()
	{
		@Override
		public void onClick(View v)
		{
			dialog.dismiss();
		}
	};

	Dialog dialog;

	private LinearLayout pathLayout;

	private static ArrayList<String> pathValues = new ArrayList<String>();

	private View closeButton;

	protected void addToPath(String currentPath)
	{
		if(!pathValues.contains(currentPath))
		{
			pathValues.add(currentPath);

		}
		refreshPath();
	}

	/**
	 * Close this activity if it is opened as a child activity.
	 * 
	 * @param resultCode
	 * @param path
	 */
	protected void closeActivity(int resultCode, String path)
	{
		setResult(resultCode);
		removeTextOnlyFromPath(path);
		finish();
	}

	protected SharedPreferences getApplicationPreferences()
	{
		return getSharedPreferences("anditssafe", MODE_PRIVATE);
	}

	protected Dialog getCustomDialog(int layoutRes, int title)
	{
		return getCustomDialog(layoutRes, getResources().getString(title));
	}

	protected Dialog getCustomDialog(int layoutRes, String title)
	{
		final Dialog customDlg = new Dialog(this);
		customDlg.requestWindowFeature(Window.FEATURE_NO_TITLE);
		customDlg.setContentView(layoutRes);
		setDialogTitle(customDlg, title);
		return customDlg;
	}

	private Dialog getExitDialog()
	{
		dialog = new Dialog(this);
		dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
		dialog.setContentView(R.layout.dialog_exitapplication);
		setDialogTitle(dialog, getResources().getString(R.string.app_name));
		Button btnYes = (Button) dialog.findViewById(R.id.yes);
		btnYes.setOnClickListener(yesListener);
		Button btnNo = (Button) dialog.findViewById(R.id.no);
		btnNo.setOnClickListener(noListener);
		return dialog;
	}

	protected Button[] getOKCancelButtons(Dialog parent)
	{
		Button[] buttons = new Button[2];
		buttons[0] = (Button) parent.findViewById(R.id.dialog_ok);
		buttons[1] = (Button) parent.findViewById(R.id.dialog_cancel);
		return buttons;
	}

	private View getPath(String pathName)
	{
		LayoutInflater inflater = getLayoutInflater();

		View path = inflater.inflate(R.layout.panel_path, null);

		TextView pathText = (TextView) path.findViewById(R.id.title);
		pathText.setText(pathName);
		return path;
	}

	protected void hideProgress()
	{
		progress.setVisibility(View.GONE);
	}

	protected void inflateMenu(Menu menu, int id)
	{
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(id, menu);
	}

	@Override
	public void onCreate(Bundle app)
	{
		super.onCreate(app);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setupUI(app);
		closeButton = findViewById(R.id.quitButton2);
		Button btnClose = (Button) findViewById(R.id.quitButton);
		btnClose.setOnClickListener(quitButtonClickListener);
		closeButton.setOnClickListener(quitButtonClickListener);
		stausText = (TextView) findViewById(R.id.footerMsg);
		progress = (ProgressBar) findViewById(R.id.pathProgress);
		pathLayout = (LinearLayout) findViewById(R.id.pathLayout);
		populateUI();
	}

	@Override
	protected Dialog onCreateDialog(int id)
	{
		switch(id)
		{
			case EXIT_APP_DIALOG:
				return getExitDialog();
		}
		return null;
	}

	/**
	 * Here's where u set the data for the widgets
	 */
	protected abstract void populateUI();

	private void refreshPath()
	{
		pathLayout.removeAllViewsInLayout();
		for(int i = 0; i < pathValues.size(); i++)
		{
			pathLayout.addView(getPath(pathValues.get(i)));
		}
	}

	// TODO UCdetector: Remove unused code:
	// protected void removeFromPath(String pathName) {
	// pathValues.remove(pathName);
	// refreshPath();
	// }

	protected void removeTextOnlyFromPath(String pathName)
	{
		boolean res = pathValues.remove(pathName);
		Log.d("UI", "Path removal->" + res);
	}

	private void setDialogTitle(Dialog dialog, String title)
	{
		((TextView) dialog.findViewById(R.id.dialogTitle)).setText(title);
	}

	protected void setStatusText(String text)
	{
		stausText.setText(text);
	}

	protected void setToPath(String currentPath)
	{
		pathValues.clear();
		pathValues.add(currentPath);
		refreshPath();
	}

	/**
	 * Here's where you make a call to <code>setContentView</code> and find the
	 * widgets needed for u to populate in {@link ParentActivity#populateUI()}
	 * 
	 * @param savedInstance
	 */
	protected abstract void setupUI(Bundle savedInstance);

	/**
	 * show the exit button in the header panel;
	 */
	protected void showExitButton()
	{
		closeButton.setVisibility(View.VISIBLE);
	}

	protected void showProgress()
	{
		progress.setVisibility(View.VISIBLE);
	}

}
